#include <vector>
#include <unordered_map>
#include<limits>

class Solution {
public:
    int minSubarray(std::vector<int>& nums, int p) {
        int mod = 0;
        for (const auto& x : nums)
        {
            mod = (mod + x)%p;
        }
        if (mod == 0) return 0;

        std::unordered_map<int, int> map;
        map.insert({0,-1});
        int ans = std::numeric_limits<int>::max();

int len = nums.size();
        int cur =0;
        int f = 0;
        for (int i = 0; i < len; i++)
        {
            cur =(cur+nums[i]) % p;
            f = cur >= mod ?(cur-mod):(cur + p -mod);
            if (map.find(f) != map.end())
            {
                ans = std::min(ans,i-map[f]);
            }
            if (map.find(cur) != map.end())
            {
                map[cur]= i;
            }else
            {
                map.insert({cur,i});
            }
           
        }
        return ans == nums.size() ? -1 : ans;
        
    }
};